package Logic;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Database.AuditLogDB;
import Database.StaffDB;
import Model.Committee;
import Model.CommitteeRole;
import Model.Staff;
/**
 * Servlet implementation class CommitteeEdit_BLL
 */

public class CommitteeEdit2_BLL extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CommitteeEdit2_BLL() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		String staffname;
		String rolename;
		String id = request.getParameter("committeeId").trim();

		
		

		if (request.getParameter("submit").equals("Save")) {

			int rows = Integer.parseInt(request.getParameter("numberOfRow").trim());
			String tempri = request.getParameter("rowindex").trim();
			int rowindex = Integer.parseInt(tempri);
			// check for same role name
			Set<String> set = new HashSet<String>();
			boolean duplicate = false;
			boolean blank = false;
			int realnumtime = 0;
			boolean bothblank = false;
			for (int i = 0; i <= rowindex; i++) {
				
				if(request.getParameter("role" + i) != null && request.getParameter("staff" + i) != null )
				{
					realnumtime ++;
				
					if (!set.add(request.getParameter("role" + i).trim())) {
						duplicate = true;
									
					}
	
					
					if (request.getParameter("role" + i).trim() == "" || request.getParameter("staff" + i).trim() == "") {
						blank = true;
						if (request.getParameter("role" + i).trim() == "" && request.getParameter("staff" + i).trim() == ""){
							bothblank = true;
						}
					}
					

				}
				
			}
			
			if(realnumtime == 1 && bothblank == true )
			{
				blank = false;
			}
			
			

			if (blank) {
				sendErrorMsg("1", id, request, response);
			}
			else if (duplicate) {
				sendErrorMsg("2", id, request, response);
			}
			else if(realnumtime == 1 && bothblank == true ) {
				Database.CommitteeRoleDB.deleteCommitteeRole(id);
				AuditLogDB.insertLog("update committee staff", (String) request
						.getSession().getAttribute("name"));
				response.sendRedirect("committee_viewcomm.jsp?error=-2");
			} else {
				Database.CommitteeRoleDB.deleteCommitteeRole(id);
				for (int i = 0; i <= rowindex; i++) {
					if(request.getParameter("role" + i) != null && request.getParameter("staff" + i) != null )
					{
					rolename = request.getParameter("role" + i).trim();
					staffname = request.getParameter("staff" + i).trim();
					
					CommitteeRole cr = new CommitteeRole();
					cr.setId(Integer.toString(i));
					cr.setCommittee_id(id);
					cr.setName(rolename);
					cr.setStaffName(staffname);
					Database.CommitteeRoleDB.createCommitteeRole(cr);
					}
				}
				AuditLogDB.insertLog("update committee staff", (String) request
						.getSession().getAttribute("name"));
				response.sendRedirect("committee_viewcomm.jsp?error=-2");
			}
		}
		else if (request.getParameter("submit").equals("Finish")) {
				
				int rows = Integer.parseInt(request.getParameter("numberOfRow").trim());
				String tempri = request.getParameter("rowindex").trim();
				int rowindex = Integer.parseInt(tempri);
				// check for same role name
				Set<String> set = new HashSet<String>();
				boolean duplicate = false;
				boolean blank = false;
				int realnumtime = 0;
				boolean bothblank = false;
				for (int i = 0; i <= rowindex; i++) {
					
					if(request.getParameter("role" + i) != null && request.getParameter("staff" + i) != null )
					{
						realnumtime ++;
						
						if (!set.add(request.getParameter("role" + i).trim())) {
							duplicate = true;
							
						}
						
						
						if (request.getParameter("role" + i).trim() == "" || request.getParameter("staff" + i).trim() == "") {
							blank = true;
							if (request.getParameter("role" + i).trim() == "" && request.getParameter("staff" + i).trim() == ""){
								bothblank = true;
							}
						}
						
						
					}
					
				}
				
				if(realnumtime == 1 && bothblank == true )
				{
					blank = false;
				}
				
				
				
				if (blank) {
					sendErrorMsgNew("1", id, request, response);
				}
				else if (duplicate) {
					sendErrorMsgNew("2", id, request, response);
				}
				else if(realnumtime == 1 && bothblank == true ) {
					Database.CommitteeRoleDB.deleteCommitteeRole(id);
					AuditLogDB.insertLog("update committee staff", (String) request
							.getSession().getAttribute("name"));
					response.sendRedirect("committee_newcomm.jsp?error=-1");
				} else {
					Database.CommitteeRoleDB.deleteCommitteeRole(id);
					for (int i = 0; i <= rowindex; i++) {
						if(request.getParameter("role" + i) != null && request.getParameter("staff" + i) != null )
						{
							rolename = request.getParameter("role" + i).trim();
							staffname = request.getParameter("staff" + i).trim();
							if (staffname.equalsIgnoreCase("Others")) {
								staffname=request.getParameter("others-input"+i).trim();
								if(!Staff.isExist(staffname))
								{
									Staff.addStaff(staffname);
								}
							}
							CommitteeRole cr = new CommitteeRole();
							cr.setId(Integer.toString(i));
							cr.setCommittee_id(id);
							cr.setName(rolename);
							cr.setStaffName(staffname);
							Database.CommitteeRoleDB.createCommitteeRole(cr);
						}
					}
					AuditLogDB.insertLog("update committee staff", (String) request
							.getSession().getAttribute("name"));
					response.sendRedirect("committee_newcomm.jsp?error=-1");
				}
			
		} else {
			
			response.sendRedirect("committee_modifycomm.jsp?action=e&id=" + id);
		}
	}

	private void sendErrorMsgNew(String error, String committeeId,
			HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		
		if( request.getParameter("rowindex") != null){
			int rowindex = Integer.parseInt(request.getParameter("rowindex").trim());	
		for (int i = 0; i <= rowindex; i++) {
			//declare arraylist for staffnamelist
			String tempreqstaf = request.getParameter("staff" + i).trim();
			if(  tempreqstaf!= null )
			{
				if(  !tempreqstaf.equals("") )
				{
				request.setAttribute("staff" + i,request.getParameter("staff" + i).trim());
				//seperate staff  by , into an array
				String[] stafflist2 = request.getParameter("staff" + i).trim().split(", ");

				ArrayList<String> staffnamelist = new ArrayList<String>();
				//send staff login id and return name and store in array
				for (int x = 0; x < stafflist2.length;  x++) {
					Staff staffname ;
					staffname = Database.CredentialDB.getStaffName(stafflist2[x]);
					staffnamelist.add(staffname.getName());
					
				}
				request.setAttribute("staffnamelist" + i,staffnamelist);
				
			}
			
			//get requests of role, staff and the staffnamelist
			//set requests of role, staff and the staffnamelist
			request.setAttribute("roleName" + i, request.getParameter("role" + i).trim());
			request.setAttribute("staffName" + i, request.getParameter("staff" + i).trim());
			}
		}
		request.setAttribute("rowindex", request.getParameter("rowindex").trim());
		request.setAttribute("commListRow", rowindex + 1);
		request.getRequestDispatcher(
				"committee_newcomm2.jsp?error=" + error + "&action=e&id="
						+ committeeId).forward(request, response);
	}

	}
	private void sendErrorMsg(String error, String committeeId,
			HttpServletRequest request, HttpServletResponse response)
					throws ServletException, IOException {
		
		
		if( request.getParameter("rowindex") != null){
			int rowindex = Integer.parseInt(request.getParameter("rowindex").trim());	
			for (int i = 0; i <= rowindex; i++) {
				//declare arraylist for staffnamelist
				String tempreqstaf = request.getParameter("staff" + i).trim();
				if(  tempreqstaf!= null )
				{
					if(  !tempreqstaf.equals("") )
					{
						request.setAttribute("staff" + i,request.getParameter("staff" + i).trim());
						//seperate staff  by , into an array
						String[] stafflist2 = request.getParameter("staff" + i).trim().split(", ");
						
						ArrayList<String> staffnamelist = new ArrayList<String>();
						//send staff login id and return name and store in array
						for (int x = 0; x < stafflist2.length;  x++) {
							Staff staffname ;
							staffname = Database.CredentialDB.getStaffName(stafflist2[x]);
							staffnamelist.add(staffname.getName());
							
						}
						request.setAttribute("staffnamelist" + i,staffnamelist);
						
					}
					
					//get requests of role, staff and the staffnamelist
					//set requests of role, staff and the staffnamelist
					request.setAttribute("roleName" + i, request.getParameter("role" + i).trim());
					request.setAttribute("staffName" + i, request.getParameter("staff" + i).trim());
				}
			}
			request.setAttribute("rowindex", request.getParameter("rowindex").trim());
			request.setAttribute("commListRow", rowindex + 1);
			request.getRequestDispatcher(
					"committee_editcomm.jsp?error=" + error + "&action=e&id="
							+ committeeId).forward(request, response);
		}
		
	}
}
